package cn.gok.mmall.service.impl;

import cn.gok.mmall.common.Const;
import cn.gok.mmall.common.ServerResponse;
import cn.gok.mmall.common.TokenCache;
import cn.gok.mmall.dao.UserMapper;
import cn.gok.mmall.pojo.User;
import cn.gok.mmall.service.IUserService;
import cn.gok.mmall.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * @Author: chenfan
 * @Description:
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        String md5Password = MD5Util.MD5EncodeUtf8(password);
        User user = userMapper.queryUserByUsernameAndPassword(username, md5Password);
        user.setPassword(null);
        return user;
    }

    @Override
    public ServerResponse addUser(User user) {

        int count = userMapper.checkUserName(user.getUsername());
        if (count > 0){
            return  ServerResponse.error("用户名已存在");
        }
        count = userMapper.checkEmail(user.getEmail());
        if (count > 0){
            return  ServerResponse.error("邮箱已存在");
        }

        //对用户的密码进行MD5加密
        String md5Password = MD5Util.MD5EncodeUtf8(user.getPassword());
        user.setPassword(md5Password);

        count = userMapper.insertSelective(user);
        if (count > 0){
            return ServerResponse.success("注册成功");
        }
        return ServerResponse.error("注册失败");
    }


    @Override
    public ServerResponse checkValid(String str, String type) {
        if (Const.USERNAME.equals(type)){
            int i = userMapper.checkUserName(str);

            if (i > 0){
                return ServerResponse.error("用户名已存在");
            }else {
                return ServerResponse.success("用户名可用");
            }
        }else if(Const.EMAIL.equals(type)){
           int i =  userMapper.checkEmail(str);
            if (i > 0){
                return ServerResponse.error("邮箱已存在");
            }else {
                return ServerResponse.success("邮箱可用");
            }
        }

        return ServerResponse.error("参数有误");
    }

    @Override
    public ServerResponse getQuestion(String username) {
        int i = userMapper.checkUserName(username);
        if(i<=0){
            return ServerResponse.error("此用户不存在");
        }

        String question = userMapper.getQuestionByUsername(username);
        if (StringUtils.isNotBlank(question)){
            return ServerResponse.success(question);
        }
        return ServerResponse.error("未设置任何问题");
    }

    @Override
    public ServerResponse checkAnswer(String username,String question,String answer){

        int i = userMapper.checkAnswer(username, question, answer);

        if (i == 1){
            String token = UUID.randomUUID().toString();
            TokenCache.addKey(username,token);
            return ServerResponse.success(token);
        }
        return ServerResponse.error("回答问题错误，请重试");
    }

    @Override
    public ServerResponse updatePasswordByUsername(String username, String newPassword,String forgetToken) {
        String serverToken = TokenCache.getKey(username);

        // 如果forgetToken为空，则提示不能修改
        if (StringUtils.isBlank(forgetToken)){
            return ServerResponse.error("修改密码失败，需要传入token值");
        }

        int i = userMapper.checkUserName(username);
        if(i<= 0){
            return ServerResponse.error("此用户不存在");
        }

        // 如果forgetToken为空，则提示不能修改
        if (StringUtils.isBlank(serverToken)){
            return ServerResponse.error("修改密码失败，token过期");
        }

        String md5Password = MD5Util.MD5EncodeUtf8(newPassword);
        i = userMapper.updatePasswordByUsername(username, md5Password);
        if (i > 0){
            TokenCache.removeKey(username);
            return ServerResponse.success("修改成功");
        }
        return ServerResponse.error("修改失败");
    }
}
